#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QString>
#include <QStringList>
#include <QMenu>


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    //设置播放器背景图
    ui->setupUi(this);
    this->setAutoFillBackground(true);
    QPalette palette;
    palette.setBrush(QPalette::Background,QBrush(QPixmap(":/images/background.png")));
    this->setPalette(palette);

    ui->volumeSlider->setRange(0,100);
    this->setWindowTitle(tr("音频播放器"));
    this->playList=new QMediaPlaylist(this);
    playList->setPlaybackMode(QMediaPlaylist::Loop);
    this->player=new QMediaPlayer(this);
    player->setPlaylist(playList);
    player->setVolume(50);//中等音量
    ui->volumeSlider->setValue(50);//音量条在中间位置
    ui->addSongsButton->setStyleSheet("QPushButton{border:1px;border-radius:10px;border-width:0px;background:#353b46}");
    ui->clearButton->setStyleSheet("QPushButton{border:1px;border-radius:10px;border-width:0px;background:#353b46}");
    ui->delectButton->setStyleSheet("QPushButton{border:1px;border-radius:10px;border-width:0px;background:#353b46}");
    ui->volumeButton->setStyleSheet("QToolButton{border:1px;border-radius:10px;border-width:0px;background:#353b46}");

    QObject::connect(ui->volumeSlider,SIGNAL(valueChanged(int)),this,SLOT(setVol(int)));
    QObject::connect(ui->addSongsButton,SIGNAL(clicked()),this,SLOT(addSongs()));
    QObject::connect(ui->playPauseButton,SIGNAL(clicked()),this,SLOT(playPause()));//
    QObject::connect(ui->stoptButton,SIGNAL(clicked()),this,SLOT(playStop()));
    QObject::connect(ui->previousButton,SIGNAL(clicked()),this,SLOT(playPrevious()));
    QObject::connect(ui->nextButton,SIGNAL(clicked()),this,SLOT(playNext()));
    QObject::connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(durChanged(qint64)));
    QObject::connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(posChanged(qint64)));
    QObject::connect(ui->processSlider,SIGNAL(valueChanged(int)),this,SLOT(setPos(int)));
    QObject::connect(ui->clearButton,SIGNAL(clicked()),this,SLOT(clearList()));
    QObject::connect(ui->delectButton,SIGNAL(clicked()),this,SLOT(delectSong()));
    QObject::connect(ui->volumeButton,SIGNAL(clicked()),this,SLOT(volumeSilence()));
    QObject::connect(playList,SIGNAL(mouseDoubleClicked()),this,SLOT(playPause()));
    ui->stoptButton->setStyleSheet("border:1px groove gray;border-radius:10px;padding:2px 4px;background:#353b46;opacity:0.2");
    ui->playPauseButton->setStyleSheet("border:1px groove gray;border-radius:10px;padding:2px 4px;background:#353b46;opacity:0.2");
    ui->previousButton->setStyleSheet("border:1px groove gray;border-radius:10px;padding:2px 4px;background:#353b46;opacity:0.2");
    ui->nextButton->setStyleSheet("border:1px groove gray;border-radius:10px;padding:2px 4px;background:#353b46;opacity:0.2");

    action_hideShow=new QAction(tr("隐藏/显示窗口部件"),this);
	action_hideShow->setShortcut(tr("Ctrl+H"));
    QObject::connect(action_hideShow,SIGNAL(triggered()),this,SLOT(hideShow()));
    action_addSongs=new QAction(tr("添加本地歌曲"),this);
    action_addSongs->setShortcut(tr("Ctrl+I"));
    QObject::connect(action_addSongs,SIGNAL(triggered()),this,SLOT(addSongs()));
    action_playPause=new QAction(tr("播放/暂停"),this);
    action_playPause->setShortcut(tr("Ctrl+Enter"));
    QObject::connect(action_playPause,SIGNAL(triggered()),this,SLOT(playPause()));
    action_stop=new QAction(tr("停止"),this);
    action_stop->setShortcut(tr("Ctrl+S"));
    QObject::connect(action_stop,SIGNAL(triggered()),this,SLOT(playStop()));
    action_previous=new QAction(tr("上一曲"),this);
    action_previous->setShortcut(tr("Ctrl+Left"));
    QObject::connect(action_previous,SIGNAL(triggered()),this,SLOT(playPrevious()));
    action_next=new QAction(tr("下一曲"),this);
    action_next->setShortcut(tr("Ctrl+Right"));
    QObject::connect(action_next,SIGNAL(triggered()),this,SLOT(playNext()));
    action_addVolume=new QAction(tr("音量+(5%)"),this);
    action_addVolume->setShortcut(tr("Ctrl+Up"));
    QObject::connect(action_addVolume,SIGNAL(triggered()),this,SLOT(addVolume()));
    action_redVolume=new QAction(tr("音量-(5%)"),this);
    action_redVolume->setShortcut(tr("Ctrl+Down"));
    QObject::connect(action_redVolume,SIGNAL(triggered()),this,SLOT(redVolume()));
    action_quit=new QAction(tr("退出"),this);
    QObject::connect(action_quit,SIGNAL(triggered()),this,SLOT(close()));
    action_playLoop=new QAction(tr("列表循环"),this);
    QObject::connect(action_playLoop,SIGNAL(triggered()),this,SLOT(playLoop()));
    action_randomMode=new QAction(tr("随机播放"),this);
    QObject::connect(action_randomMode,SIGNAL(triggered()),this,SLOT(randomMode()));
    action_sequentialMode=new QAction(tr("顺序播放"),this);
    QObject::connect(action_sequentialMode,SIGNAL(triggered()),this,SLOT(sequentialMode()));
    action_currentItemInLoop=new QAction(tr("单曲循环"),this);
    QObject::connect(action_currentItemInLoop,SIGNAL(triggered()),this,SLOT(currentItemInLoop()));

}

Widget::~Widget()
{
    delete ui;
}
//设置循环播放
void Widget::playLoop()
{
    playList->setPlaybackMode(QMediaPlaylist::Loop);
}
//设置随机播放
void Widget::randomMode()
{
    playList->setPlaybackMode(QMediaPlaylist::Random);
}
//设置顺序播放
void Widget::sequentialMode()
{
    playList->setPlaybackMode(QMediaPlaylist::Sequential);
}
//设置单曲循环
void Widget::currentItemInLoop()
{
    playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
}
//设置音量
void Widget::setVol(int vol)
{
    player->setVolume(vol);
    QString volumePercent=QString::number(ui->volumeSlider->value());
    volumePercent+='%';
    ui->volumeSlider->setStatusTip(volumePercent);
}
//添加歌曲
void Widget::addSongs()
{
    QStringList songsList=QFileDialog::getOpenFileNames(this,tr("选择文件"),QDir::homePath(),tr("*.mp3"));
    for(int i=0;i<songsList.size();i++)
    {
        QString song=songsList.at(i);//songUrl
        this->playList->addMedia(QUrl::fromLocalFile(song));
        QStringList songL=song.split('/');
        song=songL.last();//songname
        ui->songsList->addItem(song);
    }
}

//双击事件
void Widget::mouseDoubleClickEvent(QMouseEvent *mevent)
{

    emit mouseDoubleClicked();
    if(mevent->button()==Qt::LeftButton)
    {   int index=ui->songsList->currentRow();
        if(-1!=index)
        {
            playList->setCurrentIndex(index);
            player->play();
            ui->playPauseButton->setText(tr("暂停"));
            ui->playPauseButton->setIcon(QIcon(":/images/pause.png"));
        }
    }
}
//播放暂停
void Widget::playPause()
{
    if(QMediaPlayer::PausedState==player->state())      
    {
        player->play();
        ui->playPauseButton->setText(tr("暂停"));
        ui->playPauseButton->setIcon(QIcon(":/images/pause.png"));
    }
    else if(QMediaPlayer::PlayingState==player->state())
    {
        player->pause();
        ui->playPauseButton->setText(tr("播放"));
        ui->playPauseButton->setIcon(QIcon(":/images/play.png"));
    }else	//(QMediaPlayer::StoppedState==player->state())
    {
        player->play();
        ui->playPauseButton->setText(tr("暂停"));
        ui->playPauseButton->setIcon(QIcon(":/images/pause.png"));
    }
}
//播放停止
void Widget::playStop()
{

    this->player->stop();
    ui->playPauseButton->setText(tr("播放"));
    ui->playPauseButton->setIcon(QIcon(":/images/play.png"));
}
//上一曲
void Widget::playPrevious()
{
    player->stop();
    int index=playList->currentIndex();
    if(0==index)
        index=playList->mediaCount()-1;
    else
        index--;
    playList->setCurrentIndex(index);
    ui->songsList->setCurrentRow(index);
    player->play();
}
//下一曲
void Widget::playNext()
{
    player->stop();
    int index=playList->currentIndex();
    if(index==playList->mediaCount()-1)
        index=0;
    else
        index++;
    playList->setCurrentIndex(index);
    ui->songsList->setCurrentRow(index);
    player->play();

}
//歌曲播放长度
void Widget::durChanged(qint64 dur)
{
    ui->processSlider->setRange(0,dur);
    QString totalTime;
    dur/=1000;
    totalTime+=QString::number(dur/60);
    totalTime+=':';
    if(dur%60<10)
        totalTime+='0';
    totalTime+=QString::number(dur%60);
    ui->totalTimeLabel->setText(totalTime);
}
// 进度条控制
void Widget::posChanged(qint64 pos)
{
    ui->processSlider->setValue(pos);
    QString presentTime;
    pos/=1000;
    presentTime+=QString::number(pos/60);
    presentTime+=':';
    if(pos%60<10)
        presentTime+='0';
    presentTime+=QString::number(pos%60);
    ui->presentTimeLabel->setText(presentTime);
}
//设置当前进度
void Widget::setPos(int pos)
{
    player->setPosition(pos);
}
//清除歌曲列表
void Widget::clearList()
{
    playList->clear();
    ui->songsList->clear();
}
//删除歌曲列表中选定歌曲
void Widget::delectSong()
{
    int index=ui->songsList->currentRow();
    if(-1!=index)
    {
        ui->songsList->takeItem(index);
        playList->removeMedia(index);
    }
}
//静音
void Widget::volumeSilence()
{
    if(ui->volumeButton->text()==tr("音量"))
    {
        ui->volumeButton->setText(tr("静音"));
        ui->volumeSlider->setValue(0);
        ui->volumeButton->setIcon(QIcon(":/images/silence.png"));

    }
    else
    {
        ui->volumeButton->setText(tr("音量"));
        ui->volumeButton->setIcon(QIcon(":/images/volume.png"));
        ui->volumeSlider->setValue(50);
    }
}
//右键菜单
void Widget::contextMenuEvent(QContextMenuEvent*)
{
    QMenu* menu=new QMenu(this);

    //menu->addAction(action_hideShow);    
    menu->addAction(action_addSongs);
    menu->addSeparator();
    menu->addAction(action_playPause);
    menu->addAction(action_stop);
    menu->addAction(action_previous);
    menu->addAction(action_next);
    menu->addSeparator();
    menu->addAction(action_addVolume);
    menu->addAction(action_redVolume);
    menu->addSeparator();
    menu->addAction(action_playLoop);
    menu->addAction(action_randomMode);
    menu->addAction(action_sequentialMode);
    menu->addAction(action_currentItemInLoop);
    menu->addSeparator();
    menu->addAction(action_quit);
    menu->move(cursor().pos());
    menu->show();
}
//关闭主窗口
void Widget::closeEvent(QCloseEvent* event)
{
        //点击右上角关闭按钮后弹出确认关闭提示窗口
        //跳出信息框，你是否要关闭.
        auto temp = QMessageBox::information(this, "tooltip", QString::fromLocal8Bit("你是否要关闭?"), QMessageBox::Yes | QMessageBox::No);
        if (temp == QMessageBox::Yes)
        {
            // 接受了 要关闭这个窗口的事件. accept和ignore只是作为一个标志.
            event->accept();
        }
        else
        {
            //忽略了 要关闭这个窗口的事件.当前窗口就不会被关闭.
            event->ignore();
        }
}
//隐藏主窗口控件
void Widget::hideShow()
{
    if(ui->songsList->isVisible())
    {
        ui->addSongsButton->setVisible(false);
        ui->clearButton->setVisible(false);
        ui->delectButton->setVisible(false);
        ui->songsList->setVisible(false);
        ui->processSlider->setVisible(false);
        ui->presentTimeLabel->setVisible(false);
        ui->totalTimeLabel->setVisible(false);
        ui->volumeButton->setVisible(false);
        ui->volumeSlider->setVisible(false);

    }
    else
    {
        ui->addSongsButton->setVisible(true);
        ui->clearButton->setVisible(true);
        ui->delectButton->setVisible(true);
        ui->songsList->setVisible(true);
        ui->processSlider->setVisible(true);
        ui->presentTimeLabel->setVisible(true);
        ui->totalTimeLabel->setVisible(true);
        ui->volumeButton->setVisible(true);
        ui->volumeSlider->setVisible(true);
    }
}
//增大音量
void Widget::addVolume()
{
    int vol=ui->volumeSlider->value();
    vol+=5;
    if(vol>100)
        vol=100;
    ui->volumeSlider->setValue(vol);
}
//降低音量
void Widget::redVolume()
{
    int vol=ui->volumeSlider->value();
    vol-=5;
    if(vol<0)
        vol=0;
    ui->volumeSlider->setValue(vol);
}